home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / gfx / fract / FlashMandelWOS.lha / FlashMandel / Developer / Modules / React / FM_ARexx_Misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-02  |  9.9 KB  |  413 lines

  1. /*
  2.  *  FM_ARexx_Misc.c
  3.  *
  4.  *  functions for reaction-arexx.
  5.  *
  6.  *  $VER: V1.1, 19.2.2002
  7.  *  Coded by Edgar Schwan
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/tasks.h>
  12. #include <libraries/asl.h>
  13. #include <libraries/locale.h>
  14. #include <iffp/ilbmapp.h>
  15. #include <graphics/text.h>
  16. #include <intuition/classusr.h>
  17. #include <reaction/reaction.h>
  18. #include <classes/arexx.h>
  19.  
  20. #include <clib/dos_protos.h>
  21. #include <clib/exec_protos.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/asl_protos.h>
  24. #include <clib/icon_protos.h>
  25. #include <clib/gadtools_protos.h>
  26. #include <clib/locale_protos.h>
  27.  
  28. #include <pragmas/dos_pragmas.h>
  29. #include <pragmas/exec_pragmas.h>
  30. #include <pragmas/intuition_pragmas.h>
  31. #include <pragmas/asl_pragmas.h>
  32. #include <pragmas/icon_pragmas.h>
  33. #include <pragmas/gadtools_pragmas.h>
  34. #include <pragmas/locale_pragmas.h>
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38.  
  39. #include "FM_ARexx_Misc.h"
  40. #include "FlashMandel.h"
  41. #include "FM_ReactionCD.h"
  42. #include "FM_ReactionBasics.h"
  43.  
  44. #ifdef FM_AREXX_SUPPORT
  45.  
  46. /* external vars */
  47. extern Object *ARexxObj;
  48. extern struct TextAttr MYFONTSTRUCT;
  49. extern struct Catalog *CatalogPtr;
  50.  
  51. /* local prototypes */
  52. //static void LaunchIt(struct ILBMInfo *Ilbm, char *cmd, char *dir);
  53.  
  54. /* LaunchARexxScript(): Launch an arexx-script.
  55.  
  56.         SYNOPSIS: BOOL  = LaunchARexx
  57.                         (
  58.                         struct ILBMInfo  *Ilbm;
  59.                         );
  60.  
  61.         INPUTS:        Ilbm:
  62.                             Pointer to the ILBMInfo-structure.
  63.  
  64.         RETURNS:        result:
  65.                             Is TRUE if successfull.
  66. */
  67.  
  68. BOOL LaunchARexxScript(struct ILBMInfo *Ilbm)
  69. {
  70. static char ARexxDir[MAX_DIRLEN] = "FLASHMANDEL:arexx";
  71. static char ARexxFilename[MAX_FILELEN] = {0};
  72. struct FileRequester *ARexxFileReq;
  73. BOOL Success = FALSE;
  74.  
  75. if (ARexxObj) {
  76.     if (ARexxFileReq = AllocAslRequest(ASL_FileRequest, 0)) {
  77.         if (AslRequestTags(ARexxFileReq, ASLFR_Window, Ilbm->win,
  78.                                                     ASLFR_InitialLeftEdge, Ilbm->win->LeftEdge + 25,
  79.                                                     ASLFR_InitialTopEdge, Ilbm->win->TopEdge + 35,
  80.                                                     ASLFR_InitialWidth, 300,
  81.                                                     ASLFR_InitialHeight, ((Ilbm->win->Height) * 7) >> 3,
  82.                                                     ASLFR_SleepWindow, TRUE,
  83.                                                     ASLFR_TextAttr, &MYFONTSTRUCT,
  84.                                                     ASLFR_TitleText, CATSTR(TXT_SelectRxScript),
  85.                                                     ASLFR_InitialDrawer, &ARexxDir,
  86.                                                     ASLFR_InitialFile, &ARexxFilename,
  87.                                                     ASLFR_DoSaveMode, FALSE,
  88.                                                     ASLFR_RejectIcons, TRUE,
  89.                                                     TAG_END)) {
  90.             strncpy(ARexxFilename, ARexxFileReq->rf_File, MAX_FILELEN);
  91.             strncpy(ARexxDir, ARexxFileReq->rf_Dir, MAX_DIRLEN);
  92.  
  93.             LaunchIt(Ilbm, ARexxFilename, ARexxDir);
  94.             Success = TRUE;
  95.             }
  96.         FreeAslRequest (ARexxFileReq);
  97.         }
  98.     }
  99. return(Success);
  100. }
  101.  
  102. /* CreateARexxMenu(): Create ARexx-menu.
  103.  
  104.         SYNOPSIS: BOOL  = CreateARexxMenu
  105.                         (
  106.                         struct DiskObject *dobj;
  107.                         );
  108.  
  109.         INPUTS:        dobj:
  110.                             Pointer to the DiskObject-structure or NULL.
  111.  
  112.         RETURNS:        result:
  113.                             Is TRUE if successfull.
  114. */
  115. struct NewMenu *ARexxNewMenu = NULL;
  116.  
  117. BOOL CreateARexxMenu(struct DiskObject *dobj)
  118. {
  119. BOOL success = FALSE;
  120. char ttnamebuf[50];
  121. const char title_txt[] = "ARexx";
  122. const char *launch_txt = (const char *) CATSTR(TXT_Launch);
  123. int numcmds = 0;
  124. ULONG i;
  125. size_t nm_size = 0, string_size = 0;
  126. struct DiskObject *cli_dobj = NULL, *act_dobj = NULL;
  127. struct Task *own_task = FindTask(NULL);
  128.  
  129. if (ARexxObj) {
  130.     if (dobj) act_dobj = dobj;
  131.     else {
  132.         if (cli_dobj = GetDiskObject(own_task->tc_Node.ln_Name)) act_dobj = cli_dobj;
  133.         else return(success);
  134.         }
  135.  
  136.     while(1) {
  137.         STRPTR value;
  138.         sprintf((STRPTR) &ttnamebuf, "REXXCMD%d", numcmds + 1);
  139.         if (!(value = FindToolType(act_dobj->do_ToolTypes, (STRPTR) &ttnamebuf))) break;
  140.         string_size += strlen(value) + 1;
  141.         numcmds++;
  142.         }
  143.     string_size += strlen((const char *) &title_txt) + 1;
  144.     string_size += strlen(launch_txt) + 1;
  145.     if (numcmds) numcmds++;
  146.     nm_size = ((numcmds + 3) * sizeof(struct NewMenu));
  147.  
  148.     if (ARexxNewMenu = (struct NewMenu *) AllocVec((ULONG) (nm_size + string_size), MEMF_PUBLIC|MEMF_CLEAR)) {
  149.         char *string = (char *) ((ULONG) ARexxNewMenu) + nm_size, *tmp;
  150.         struct NewMenu *act_nm = ARexxNewMenu;
  151.  
  152.         strcpy(string, title_txt);        /* initialize menu-title */
  153.         act_nm->nm_Type = NM_TITLE;
  154.         act_nm->nm_Label = string;
  155.         act_nm->nm_CommKey = NULL;
  156.         act_nm->nm_Flags = 0;
  157.         act_nm->nm_MutualExclude = 0;
  158.         act_nm->nm_UserData = (APTR) (REXX_MENU_ID);
  159.         string = (char *) ((ULONG) string + strlen(string)) + 1;
  160.         act_nm++;
  161.  
  162.         if (numcmds) {        /* initialize command-items */
  163.             for (i = 0; i < (numcmds - 1); i++) {
  164.                 sprintf((STRPTR) &ttnamebuf, "REXXCMD%d", i + 1);
  165.                 strcpy(string, (const char *) FindToolType(act_dobj->do_ToolTypes, (STRPTR) &ttnamebuf));
  166.                 tmp = string;
  167.                 string = (char *) ((ULONG) string + strlen(string)) + 1;
  168.                 if (tmp[strlen(tmp) -1] == '"') tmp[strlen(tmp) -1] = 0;        /* delete " */
  169.                 if (tmp[0] == '"') {tmp[0] = 0; tmp++;}
  170.                 act_nm->nm_Type = NM_ITEM;
  171.                 act_nm->nm_Label = tmp;
  172.                 act_nm->nm_CommKey = NULL;
  173.                 act_nm->nm_Flags = 0;
  174.                 act_nm->nm_MutualExclude = 0;
  175.                 act_nm->nm_UserData = (APTR) (REXX_MENU_ID + i + 1);
  176.  
  177.                 act_nm++;
  178.                 }
  179.             act_nm->nm_Type = NM_ITEM;        /* initialize barlabel */
  180.             act_nm->nm_Label = NM_BARLABEL;
  181.             act_nm->nm_CommKey = NULL;
  182.             act_nm->nm_Flags = 0;
  183.             act_nm->nm_MutualExclude = 0;
  184.             act_nm->nm_UserData = NULL;
  185.             act_nm++;
  186.             }
  187.  
  188.         strcpy(string, launch_txt);        /* initialize launch-item */
  189.         act_nm->nm_Type = NM_ITEM;
  190.         act_nm->nm_Label = string;
  191.         act_nm->nm_CommKey = NULL;
  192.         act_nm->nm_Flags = 0;
  193.         act_nm->nm_MutualExclude = 0;
  194.         act_nm->nm_UserData = (APTR) REXX_MENUITEM_LAUNCH;
  195.         act_nm++;
  196.  
  197.         act_nm->nm_Type = NM_END;        /* initialize menu-end */
  198.         act_nm->nm_Label = NULL;
  199.         act_nm->nm_CommKey = NULL;
  200.         act_nm->nm_Flags = 0;
  201.         act_nm->nm_MutualExclude = 0;
  202.         act_nm->nm_UserData = NULL;
  203.         success = TRUE;
  204.         }
  205.  
  206.     if (cli_dobj) {FreeDiskObject(cli_dobj); cli_dobj = NULL;}
  207.     }
  208. return(success);
  209. }
  210.  
  211. /* SetARexxMenu(): Activate menu-items for ARexx-menu in main-window.
  212.  
  213.         SYNOPSIS: BOOL  = SetARexxMenu
  214.                         (
  215.                         struct ILBMInfo  *Ilbm;
  216.                         );
  217.  
  218.         INPUTS:        Ilbm:
  219.                             Pointer to the ILBMInfo-structure.
  220.  
  221.         RETURNS:        result:
  222.                             Is TRUE if successfull.
  223. */
  224. struct Menu *ARexxMenu = NULL;
  225.  
  226. BOOL SetARexxMenu(struct ILBMInfo *Ilbm)
  227. {
  228. BOOL success = FALSE;
  229. struct Menu *win_menu = Ilbm->win->MenuStrip, *last_menu = NULL;
  230. APTR vi = NULL;
  231.  
  232. if (ARexxObj) {
  233.     if (vi = GetVisualInfoA(Ilbm->scr, NULL)) {
  234.         if (ARexxNewMenu) {
  235.             if (ARexxMenu = CreateMenusA(ARexxNewMenu, NULL)) {
  236.                 last_menu = win_menu;
  237.                 while (last_menu->NextMenu) {
  238.                     last_menu = last_menu->NextMenu;
  239.                     }
  240.                 ModifyIDCMP(Ilbm->win, NULL);
  241.                 ClearMenuStrip(Ilbm->win);
  242.                 last_menu->NextMenu = ARexxMenu;
  243.                 LayoutMenus(win_menu, vi, GTMN_TextAttr, &MYFONTSTRUCT, GTMN_NewLookMenus, TRUE, TAG_END);
  244.                 SetMenuStrip(Ilbm->win, win_menu);
  245.                 ModifyIDCMP(Ilbm->win, IDCMP_STANDARD);
  246.                 success = TRUE;
  247.                 }
  248.             }
  249.         FreeVisualInfo(vi);
  250.         }
  251.     }
  252. return(success);
  253. }
  254.  
  255. /* RemoveARexxMenu(): Remove and free menu-items for ARexx-menu.
  256.  
  257.         SYNOPSIS: void  = RemoveARexxMenu
  258.                         (
  259.                         struct ILBMInfo  *Ilbm;
  260.                         );
  261.  
  262.         INPUTS:        Ilbm:
  263.                             Pointer to the ILBMInfo-structure.
  264.  
  265.         RETURNS:        result: -
  266. */
  267.  
  268. void RemoveARexxMenu(struct ILBMInfo *Ilbm)
  269. {
  270. struct Menu *win_menu = Ilbm->win->MenuStrip, *act_menu = NULL, *prev_menu = NULL;
  271. APTR vi = NULL;
  272.  
  273. if (ARexxMenu) {
  274.     if (vi = GetVisualInfoA(Ilbm->scr, NULL)) {
  275.         act_menu = win_menu;
  276.         while (act_menu) {
  277.             if (act_menu == ARexxMenu) {
  278.                 ModifyIDCMP(Ilbm->win, NULL);
  279.                 ClearMenuStrip(Ilbm->win);
  280.                 if (prev_menu) prev_menu->NextMenu = ARexxMenu->NextMenu;
  281.                 LayoutMenus(win_menu, vi, GTMN_TextAttr, &MYFONTSTRUCT, GTMN_NewLookMenus, TRUE, TAG_END);
  282.                 SetMenuStrip(Ilbm->win, win_menu);
  283.                 ModifyIDCMP(Ilbm->win, IDCMP_STANDARD);
  284.  
  285.                 FreeMenus(ARexxMenu); ARexxMenu = NULL;
  286.                 break;
  287.                 }
  288.             prev_menu = act_menu;
  289.             act_menu = act_menu->NextMenu;
  290.             }
  291.         FreeVisualInfo(vi);
  292.         }
  293.     }
  294. }
  295.  
  296. /* FreeARexxMenu(): Free menu-items of arexx-menu.
  297.  
  298.         SYNOPSIS: void  = FreeARexxMenu
  299.                         (
  300.                         void
  301.                         );
  302.  
  303.         INPUTS:        -
  304.  
  305.         RETURNS:        result: -
  306. */
  307.  
  308. void FreeARexxMenu(void)
  309. {
  310. if (ARexxNewMenu) {FreeVec((APTR) ARexxNewMenu); ARexxNewMenu = NULL;}
  311. }
  312.  
  313. /* HandleARexxMenu(): Handle all events on the arexx-menu.
  314.  
  315.         SYNOPSIS: void  = HandleARexxMenu
  316.                         (
  317.                         struct ILBMInfo *Ilbm;
  318.                         UWORD MenuCode;
  319.                         );
  320.  
  321.         INPUTS:        Ilbm:
  322.                             Pointer to the ILBMInfo-structure.
  323.  
  324.                         MenuCode:
  325.                             Code, that specifies the selected menu-item.
  326.  
  327.         RETURNS:        result: -
  328. */
  329.  
  330. void HandleARexxMenu(struct ILBMInfo *Ilbm, UWORD MenuCode)
  331. {
  332. struct MenuItem *SelectedItem = ItemAddress(Ilbm->win->MenuStrip, MenuCode);
  333. ULONG id = (ULONG) GTMENUITEM_USERDATA(SelectedItem);
  334.  
  335. if (ARexxObj) {
  336.     if (id) {
  337.         if (id & REXX_MENU_CMD_MASK) {
  338.             struct IntuiText *it = (struct IntuiText *) SelectedItem->ItemFill;
  339.             char *command = it->IText;
  340.  
  341.             LaunchIt(Ilbm, command, "FLASHMANDEL:arexx");
  342.             }
  343.         else {
  344.             switch(id) {
  345.                 case REXX_MENUITEM_LAUNCH:
  346.                     LaunchARexxScript(Ilbm);
  347.                     break;
  348.                 default:
  349.                     DisplayBeep(Ilbm->scr);
  350.                 }
  351.             }
  352.         }
  353.     }
  354. }
  355.  
  356. /* LaunchIt(): Launch an arexx-script.
  357.  
  358.         SYNOPSIS: static void  = LaunchIt
  359.                         (
  360.                         struct ILBMInfo *Ilbm;
  361.                         char *cmd;
  362.                         char *dir;
  363.                         );
  364.  
  365.         INPUTS:        Ilbm:
  366.                             Pointer to the ILBMInfo-structure.
  367.  
  368.                         cmd:
  369.                             Pointer to the command-string.
  370.  
  371.                         dir:
  372.                             Pointer to the directory-string.
  373.  
  374.         RETURNS:        result: -
  375. */
  376.  
  377. void LaunchIt(struct ILBMInfo *Ilbm, char *cmd, char *dir)
  378. {
  379. STRPTR hostname;
  380. char cmd_string[300];
  381. BPTR OldDir, NewDir, Script;
  382.  
  383. if (ARexxObj) {
  384.     GetAttr(AREXX_HostName, ARexxObj, (ULONG *) &hostname);
  385.  
  386.     sprintf((STRPTR) &cmd_string, "%s %s", cmd, hostname);
  387.     if (NewDir = Lock(dir, SHARED_LOCK)) {
  388.         OldDir = CurrentDir(NewDir);
  389.         if (Script = Lock(cmd, SHARED_LOCK)) {
  390.             LONG result1, result2, result;
  391.             STRPTR result_str;
  392.             struct apExecute ape = {
  393.                 AM_EXECUTE,
  394.                 (STRPTR) cmd_string,
  395.                 NULL,
  396.                 &result1,
  397.                 &result2,
  398.                 &result_str,
  399.                 NULL};
  400.             UnLock(Script);
  401.             result = DoMethodA(ARexxObj, (Msg) &ape);
  402.             }
  403. //        else DisplayBeep(Ilbm->scr);
  404.         CurrentDir(OldDir);
  405.         UnLock(NewDir);
  406.         }
  407.     }
  408. }
  409.  
  410. #else /* FM_AREXX_SUPPORT */
  411. extern struct Library *ResourceBase;  /* dummy */
  412. #endif /* FM_AREXX_SUPPORT */
  413.